﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media.Animation;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace BallMadness
{
    public class Worm
    {
        int currentFrame;
        TimeSpan frameLength = new TimeSpan(0, 0, 0, 0, 350);
        TimeSpan nextFrame;

        public const int POINTS = 10;

        Vector2 velocity = Vector2.Zero;
        public Vector2 position = Vector2.Zero;
        public static Random randPosition;

        private const int spawnBorder = 50;

        public Worm()
        {
            if (randPosition == null)
                randPosition = new Random();

            position.X = randPosition.Next(spawnBorder,
                SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width - spawnBorder);
            position.Y = randPosition.Next(spawnBorder,
                SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height - spawnBorder);

            velocity.X = randPosition.Next(-50, 50);
            velocity.Y = randPosition.Next(-50, 50);
        }

        public void OnDraw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(
                ScoreManager.worm,
                position,
                new Rectangle(
                    ScoreManager.worm.Width / 2 * currentFrame,
                    0,
                    ScoreManager.worm.Width / 2,
                    ScoreManager.worm.Height),
                Color.White,
                0.0f,
                new Vector2(ScoreManager.worm.Width / 4, ScoreManager.worm.Height),
                1.0f,
                SpriteEffects.None,
                1.0f);
        }

        public void OnUpdate(GameTimerEventArgs e)
        {
            nextFrame -= e.ElapsedTime;
            if (nextFrame <= TimeSpan.Zero)
            {
                nextFrame = frameLength;
                currentFrame++;
                if (currentFrame > 1) currentFrame = 0;
            }

            position.X += velocity.X * (float) e.ElapsedTime.TotalSeconds;
            position.Y += velocity.Y *(float) e.ElapsedTime.TotalSeconds;
        }

        public void OnNavigatedTo(ContentManager contentManager)
        {
        }

        public bool isOffScreen()
        {
            if (position.X > SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width + ScoreManager.worm.Width)
                return true;
            else if (position.X < 0 - ScoreManager.worm.Width)
                return true;
            else if (position.Y > SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height + ScoreManager.worm.Height)
                return true;
            else if (position.Y < 0 - ScoreManager.worm.Height)
                return true;
            else
                return false;
        }

    }
}
